﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class SettingService : ISettingService
    {
        /// <summary>
        /// 添加配置
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="describle">描述</param>
        /// <returns></returns>
        public long AddNew(string key, string value, string describle)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Setting> baseService = new BaseService<Setting>(context);

                bool isExists = baseService.GetAll().Any(s => s.Key == key);

                if (isExists)
                {
                    throw new ArgumentException(string.Format($"配置：{key}已存在"));
                }

                Setting setting = new Setting()
                {
                    Key = key,
                    Value = value,
                    Describle = describle
                };

                context.Settings.Add(setting);

                context.SaveChanges();

                return setting.Id;
            }
        }


        /// <summary>
        /// 获取所有配置
        /// </summary>
        /// <returns></returns>
        public SettingDTO[] GetAll()
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Setting> baseService = new BaseService<Setting>(context);

                return baseService.GetAll().ToList()
                    .Select(s => ToDTO(s)).ToArray();
            }
        }

        /// <summary>
        /// 根据 配置Id获取配置
        /// </summary>
        /// <param name="settingId"></param>
        /// <returns></returns>
        public SettingDTO GetSettingById(long settingId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Setting> baseService = new BaseService<Setting>(context);

                var setting = baseService.GetById(settingId);

                return setting == null ? null : ToDTO(setting);
            }
        }

        /// <summary>
        /// 根据键获取key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public SettingDTO GetSettingByKey(string key)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Setting> baseService = new BaseService<Setting>(context);

                var setting = baseService.GetAll()
                    .SingleOrDefault(s => s.Key == key);

                return setting == null ? null : ToDTO(setting);
            }
        }

        /// <summary>
        /// 软删除数据
        /// </summary>
        /// <param name="settingId"></param>
        public void MarkDeleted(long settingId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Setting> baseService = new BaseService<Setting>(context);

                Setting setting = baseService.GetById(settingId);

                if (setting == null)
                {
                    throw new ArgumentException(string.Format($"配置Id为：{settingId}不存在"));
                }

                baseService.MakeDelete(settingId);
            }
        }

        /// <summary>
        /// 更新配置
        /// </summary>
        /// <param name="settingId">配置Id</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="describle">描述</param>
        public void Update(long settingId, string key, string value, string describle)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Setting> baseService = new BaseService<Setting>(context);

                Setting setting = baseService.GetById(settingId);

                if (setting == null)
                {
                    throw new ArgumentException(string.Format($"配置Id为：{settingId}不存在"));
                }

                setting.Key = key;
                setting.Value = value;
                setting.Describle = describle;

                context.SaveChanges();
            }
        }

        private SettingDTO ToDTO(Setting setting)
        {
            return new SettingDTO()
            {
                Id = setting.Id,
                Key = setting.Key,
                Value = setting.Value,
                Describle = setting.Describle,
                CreateDateTime = setting.CreateDateTime
            };
        }
    }
}
